package com.sunsharing.dispatch.flowExtend;

import com.sunsharing.common.Context;
import com.sunsharing.common.ContextCreate;
import com.sunsharing.common.anno.Factory;
import com.sunsharing.dispatch.Dispatch;
import com.sunsharing.flow.event.ApplicationEvent;
import com.sunsharing.flow.event.NodeLeaveEvent;
import com.sunsharing.flow.eventman.ApplicationListener;
import com.sunsharing.flow.eventman.EventManager;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-5-6
 * Time: 下午3:51
 * To change this template use File | Settings | File Templates.
 */
@Factory(key="FlowEndLisener")
public class FlowEndLisener implements ApplicationListener {
    /**
     * 功能描述:
     *     处理事件
     * @param event ApplicationEvent
     */
    public void onApplicationEvent(Object obj, ApplicationEvent event)
    {
        System.out.println("----流程结束----");
        NodeLeaveEvent e = (NodeLeaveEvent)event;
        String output = e.getExecutionContext().getToken().getProcessInstance().getDefinition().getOutput();
        String oStr = (String)e.getExecutionContext().getToken().getPar(output);
        String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Result>\n" +
                "<SenderId>"+e.getExecutionContext().getToken().reqId+"</SenderId>\n" +
                "<ServiceId>XM.GOV.YZ.RS.yljz.cbryxx</ServiceId>\n" +
                "<Status>000</Status>\n" +
                "<ErrorMsg></ErrorMsg>\n" +
                "<Output type=\"string\">"+ oStr+
                "</Output>\n" +
                "</Result>\n";


        Context restContext = ContextCreate.createResultContext(result,
                e.getExecutionContext().getContext());
        new Dispatch().run(restContext);
        return;
        //EventManager.getInstance().removeApplicationListener(obj,this);
    }
}
